<template>
  <div class="container" v-if="!$_.isEmpty(info)">
    <div class="top">
      <div class="top-1">
        <div class="title-box">
          <span class="title-ch">{{ info.name }}</span>
          <span class="code">{{ info.no }}</span>
        </div>
        <div class="tag-box" v-if="info.tags">
          <van-tag
            plain
            type="primary"
            v-for="(tag, index) in info.tags.split(',')"
            :key="index"
            >{{ tag }}</van-tag
          >
        </div>
      </div>
      <div class="top-2">
        <div class="top-info">
          <div class="item">
            <div class="percent" style="color: #dd4239">
              {{ info.rangeIndex }}
            </div>
            <div class="text">{{ info.rangeText }}</div>
          </div>
          <div class="item">
            <div class="percent">{{ info.netValueNew || "0.00" }}</div>
            <div class="text">最新净值</div>
          </div>
          <div class="item">
            <div class="percent" style="color: #68894e">
              {{ info.rangeIndexDay || "0.00%" }}
            </div>
            <div class="text">日涨跌</div>
          </div>
        </div>
        <div class="top-date">
          <div class="date-info">
            <span>发布日期：</span>
            <span>{{ $moment(info.createTime).format("YYYY-MM-DD") }}</span>
          </div>
          <div class="date-info">
            <span>已发布：</span>
            <span
              >{{
                $moment().diff(info.createTime, "years") > 0
                  ? $moment().diff(info.createTime, "years") + "年"
                  : ""
              }}{{
                $moment().diff(info.createTime, "days") > 0
                  ? $moment().diff(info.createTime, "days") + "天"
                  : "今天"
              }}</span
            >
          </div>
        </div>
      </div>
    </div>
    <div class="middle">
      <div class="title-box">
        <div class="left">组合介绍</div>
        <div
          class="right"
          @click="$router.push(`/productCombDetail/${info.id}`)"
        >
          <span>组合主页</span>
          <van-icon name="arrow" size="14px" />
        </div>
      </div>
      <div class="middle-info">
        {{ info.combinationDesc }}
      </div>
    </div>
    <div class="bottom-box">
      <div class="title-box">收益走势</div>
      <div class="bottom-info">
        <div class="lengend-box">
          <div class="left">
            <div class="dot"></div>
            <span>{{ info.name }}</span>
            <span>{{ info.revenueIndex }}</span>
          </div>
          <!-- <div class="right">
                        <div class="dot"></div>
                        <span>上证综指：</span>
                        <span>+0.29%</span>
                    </div> -->
        </div>
        <div class="tab-box">
          <van-tabs
            type="card"
            color="#B49F99"
            v-model="active"
            @change="changeTime"
          >
            <van-tab title="成立以来"
              ><ve-line
                :extend="chartExtend"
                :colors="['#F63E33', '#83B4E6']"
                :data="chartData"
                v-if="graphList.length > 0"
              />
              <div class="bottom-info" v-else>
                <div class="empty">暂无收益走势!</div>
              </div></van-tab
            >
            <van-tab title="近一月"
              ><ve-line
                :extend="chartExtend"
                :colors="['#F63E33', '#83B4E6']"
                :data="chartData"
                v-if="graphList.length > 0"
              />
              <div class="bottom-info" v-else>
                <div class="empty">暂无收益走势!</div>
              </div></van-tab
            >
            <van-tab title="近六月"
              ><ve-line
                :extend="chartExtend"
                :colors="['#F63E33', '#83B4E6']"
                :data="chartData"
                v-if="graphList.length > 0"
              />
              <div class="bottom-info" v-else>
                <div class="empty">暂无收益走势!</div>
              </div></van-tab
            >
            <van-tab title="近一年"
              ><ve-line
                :extend="chartExtend"
                :colors="['#F63E33', '#83B4E6']"
                :data="chartData"
                v-if="graphList.length > 0"
              />
              <div class="bottom-info" v-else>
                <div class="empty">暂无收益走势!</div>
              </div></van-tab
            >
            <van-tab title="近三年"
              ><ve-line
                :extend="chartExtend"
                :colors="['#F63E33', '#83B4E6']"
                :data="chartData"
                v-if="graphList.length > 0"
              />
              <div class="bottom-info" v-else>
                <div class="empty">暂无收益走势!</div>
              </div></van-tab
            >
          </van-tabs>
        </div>
      </div>
    </div>
    <div class="fixed">
      <van-button block type="primary" @click="showBuyModal = true">
        购买指引
      </van-button>
    </div>
    <guide-modal :info="guideInfo" :show="showBuyModal" @close="close" />
  </div>
</template>
<script>
import { getDetail, getGraphData, getBuyGuideDetail } from "@/api/v1/product";
import GuideModal from "./guideModal.vue";
export default {
  data() {
    return {
      active: 0,
      chartData: {
        columns: [],
        rows: [],
      },
      chartExtend: {
        legend: {
          show: false,
        },
        yAxis: {
          type: "value",
          axisLabel: {
            formatter: function(val, index) {
              return val > 0 ? `+${val}%` : `-${val}%`;
            },
          },
        },
        tooltip: {
          formatter: function(params) {
            let html = params[0].name + "<br>";
            for (let i = 0; i < params.length; i++) {
              html +=
                params[i].marker +
                params[i].seriesName +
                ":" +
                (params[i].value[1] > 0
                  ? `+${params[i].value[1]}`
                  : `-${params[i].value[1]}`);
              html += "%";
            }
            return html + "<br>";
          },
        },
      },
      info: {},
      graphList: [],
      id: "",
      showBuyModal: false,
      guideInfo: {},
    };
  },
  components: { GuideModal },
  async created() {
    let { id } = this.$route.params;
    let { userId } = this.$route.query;
    let res = await getBuyGuideDetail({ productId: id });
    if (res) this.guideInfo = res;
    let info = await getDetail({ id });
    if (!this.$_.isEmpty(info)) {
      this.info = info;
      this.id = id;
    }
    this.getGraphList(id, info.name);
  },
  methods: {
    changeTime() {
      this.getGraphList();
    },
    async getGraphList(id, name) {
      let _this = this;
      let _name = name || this.info.name;
      let graphList = await getGraphData({
        id: id || this.id,
        type: this.active,
      });
      if (graphList) {
        this.graphList = graphList;
        let graphRows = graphList.map((item) => ({
          日期: item.date,
          [_name]: item.value,
        }));
        let graphColumns = ["日期", _name];
        this.chartData = {
          columns: graphColumns,
          rows: graphRows,
        };
      }
    },
    close(show){
        this.showBuyModal=show
    }
  },
};
</script>
<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100vh;
  background: #f8f8f8;
  .top {
    background: linear-gradient(90deg, #efebe8 0%, #fefcfd 100%);
    padding-bottom: 10px;
    .top-1 {
      background: linear-gradient(90deg, #9d877a 0%, #b6a9a0 20%, #9d877a 100%);
      padding: 20px 30px;
      color: #fff;
      & > .title-box {
        margin-bottom: 10px;

        .title-ch {
          font-size: 18px;
          font-weight: bold;
        }
        .code {
          font-size: 14px;
          margin-left: 15px;
        }
      }
      .tag-box {
        margin-left: -10px;
        .van-tag--primary.van-tag--plain {
          color: #f8e9e2;
          background: #a28c80;
          margin-left: 10px;
          padding: 4px 10px;
        }
      }
    }
    .top-2 {
      padding-left: 20px;
      .top-info {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 20px 0;
        padding-right: 20px;
        border-bottom: 1px dashed #a4a2a3;
        .item {
          text-align: center;
          margin-left: 15px;
          .percent {
            font-size: 24px;
          }
          .text {
            color: #837f7e;
            font-size: 12px;
            margin-top: 6px;
          }
        }
      }
      .top-date {
        margin-top: 10px;
        display: flex;
        justify-content: space-between;
        .date-info {
          font-size: 12px;
          padding-right: 10px;
          span:nth-child(1) {
            color: #a18c88;
          }
        }
      }
    }
  }
  .middle,
  .bottom-box {
    margin-top: 20px;
    padding: 20px 10px;
    background: #fff;
    & > .title-box {
      font-size: 18px;
      font-weight: bold;
      border-bottom: 1px solid #f4f4f4;
      padding-bottom: 15px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .right {
        display: flex;
        align-items: center;
        font-size: 14px;
        font-weight: normal;
        color: #61617b;
      }
    }
    .middle-info,
    .bottom-info {
      padding-top: 15px;
      font-size: 14px;
      line-height: 2;
    }
  }
  .bottom-info {
    .lengend-box {
      display: flex;
      justify-content: space-between;
      padding: 0 20px;
      .left,
      .right {
        display: flex;
        align-items: center;
        font-size: 12px;
        .dot {
          width: 6px;
          height: 6px;
          border-radius: 50%;
          margin-right: 3px;
        }
      }
      .left {
        .dot {
          background: #f63e33;
        }
        span:nth-last-child(1) {
          color: #f63e33;
        }
      }
      .right {
        .dot {
          background: #83b4e6;
        }
        & > span:nth-last-child(1) {
          color: rgb(104, 137, 78);
        }
      }
    }
    .tab-box {
      margin-top: 20px;
      .empty {
        color: #888;
        font-size: 14px;
        text-align: center;
      }
    }
    /deep/ .van-tabs__nav--card .van-tab:last-child {
      white-space: nowrap;
    }
  }
  .fixed {
    position: fixed;
    bottom: 0;
    width: 100%;
    button {
      width: 100%;
    }
    /deep/.van-button--primary {
      background-color: #c90302;
      border: none;
    }
  }
}
</style>
